package mi.payment.ui.admin;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.TransactionInputPwd;
import mi.payment.settings.TransactionInputPwdOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.title.Title;

/**
 * 交易输密控制
 * Created by lj on 2017/2/13.
 */
public class TransactionEncryptionSettingActivity extends ParentActivity {
    private View mVoidTransactionPassword,mCancelTransactionPassword,mCompleteVoidTransactionPassword,mAuthCompleteTransactionPassword;
    private ButtonSwitch mVoidTransactionPasswordSwitch,mCancelTransactionPasswordSwitch,mCompleteVoidTransactionPasswordSwitch,mAuthCompleteTransactionPasswordSwitch;
    private TransactionInputPwdOperation mTransactionInputPsdOperation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction_encryption_setting);
        mTransactionInputPsdOperation = TransactionInputPwdOperation.getInstance();
        initView();
        setClickListener();
        setCheckChangeListener();
        updateUI();
    }

    /**
     * 初始化控件
     */
    private void initView(){
        Title mTitle;
        TextView mLeftText;

        mTitle = (Title) findViewById(R.id.transaction_encryption_setting_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.transaction_encryption_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransactionEncryptionSettingActivity.this.finish();
            }
        }, null, null, null);

        mVoidTransactionPassword = findViewById(R.id.void_transaction_password);
        mCancelTransactionPassword = findViewById(R.id.cancel_transaction_password);
        mCompleteVoidTransactionPassword = findViewById(R.id.complete_void_transaction_password);
        mAuthCompleteTransactionPassword = findViewById(R.id.auth_complete_transaction_password);

        mLeftText = (TextView) mVoidTransactionPassword.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.void_transaction_password));

        mLeftText = (TextView) mCancelTransactionPassword.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.cancel_transaction_password));

        mLeftText = (TextView) mCompleteVoidTransactionPassword.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.complete_void_transaction_password));

        mLeftText = (TextView) mAuthCompleteTransactionPassword.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.auth_complete_transaction_password));

        mVoidTransactionPasswordSwitch = (ButtonSwitch) mVoidTransactionPassword.findViewById(R.id.set_switch);
        mCancelTransactionPasswordSwitch = (ButtonSwitch) mCancelTransactionPassword.findViewById(R.id.set_switch);
        mCompleteVoidTransactionPasswordSwitch = (ButtonSwitch) mCompleteVoidTransactionPassword.findViewById(R.id.set_switch);
        mAuthCompleteTransactionPasswordSwitch = (ButtonSwitch) mAuthCompleteTransactionPassword.findViewById(R.id.set_switch);
    }

    /**
     * 设置点击事件
     */
    private void setClickListener(){
        mVoidTransactionPassword.setOnClickListener(this);
        mCancelTransactionPassword.setOnClickListener(this);
        mCompleteVoidTransactionPassword.setOnClickListener(this);
        mAuthCompleteTransactionPassword.setOnClickListener(this);
    }

    /**
     * 设置SwitchButton选择事件
     */
    private void setCheckChangeListener(){
        mVoidTransactionPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionInputPsdOperation.saveConsumptionRevokePwd(isChecked);
            }
        });

        mCancelTransactionPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionInputPsdOperation.saveFieldAuthRevokePwd(isChecked);
            }
        });

        mCompleteVoidTransactionPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionInputPsdOperation.saveFieldAuthCompleteRevokePwd(isChecked);
            }
        });

        mAuthCompleteTransactionPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionInputPsdOperation.saveFieldAuthCompleteReqPwd(isChecked);
            }
        });
        updateUI();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.void_transaction_password:
                //消费撤销密码
                mVoidTransactionPasswordSwitch.setOnCheck(!mVoidTransactionPasswordSwitch.isChecked());
                break;
            case R.id.cancel_transaction_password:
                //预授权撤销密码
                mCancelTransactionPasswordSwitch.setOnCheck(!mCancelTransactionPasswordSwitch.isChecked());
                break;
            case R.id.complete_void_transaction_password:
                //预授权完成撤销密码
                mCompleteVoidTransactionPasswordSwitch.setOnCheck(!mCompleteVoidTransactionPasswordSwitch.isChecked());
                break;
            case R.id.auth_complete_transaction_password:
                //预授权完成（请求）密码
                mAuthCompleteTransactionPasswordSwitch.setOnCheck(!mAuthCompleteTransactionPasswordSwitch.isChecked());
                break;
        }
    }

    private void updateUI(){
        TransactionInputPwd transactionInputPsd = mTransactionInputPsdOperation.getTransactionInputPwd();
        mVoidTransactionPasswordSwitch.setOnCheck(transactionInputPsd.isConsumptionRevokePsd());

        mCancelTransactionPasswordSwitch.setOnCheck(transactionInputPsd.isFieldAuthRevokePsd());

        mCompleteVoidTransactionPasswordSwitch.setOnCheck(transactionInputPsd.isFieldAuthCompleteRevokePsd());

        mAuthCompleteTransactionPasswordSwitch.setOnCheck(transactionInputPsd.isFieldAuthCompleteReqPsd());
    }
}
